/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode LTNode;
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
    if (head == NULL)
        return NULL;
    int cnt = 0;
    LTNode* cur = head;
    while (cur)
    {
        cnt++;
        cur = cur->next;
    }
    LTNode* dummy = (LTNode*)malloc(sizeof(LTNode));
    dummy->val = 0;
    dummy->next = head;    
    cnt -= n;
    cur = head;
    LTNode* pre = dummy;
    while (cnt--)
    {
        pre = cur;
        cur = cur->next;
    }
    pre->next = cur->next;
    free(cur);
    cur = NULL;
    return dummy->next;
}